package com.cdck.safe.dete.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cdck.safe.dete.common.result.PageResult;
import com.cdck.safe.dete.common.result.Result;
import com.cdck.safe.dete.model.entity.BookRecord;
import com.cdck.safe.dete.model.query.BaseKeywordsQuery;
import com.cdck.safe.dete.model.vo.BookRecordVO;
import com.cdck.safe.dete.model.vo.MyBookRecordVO;
import com.cdck.safe.dete.service.BookRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * (BookRecord)表控制层
 *
 * @author wp
 * @since 2024-02-25 16:48:35
 */
@Api(value = "bookRecord")
@RestController
@RequestMapping("bookRecord")
@RequiredArgsConstructor
public class BookRecordController {

    private final BookRecordService bookRecordService;


    @ApiOperation(value = "分页列表")
    @GetMapping("/page")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "queryParams"  ,value ="查询条件" )
    })
    public PageResult<BookRecordVO> queryByPage(BaseKeywordsQuery queryParams) {
        IPage<BookRecordVO> result = bookRecordService.queryByPage(queryParams);
        return PageResult.success(result);

    }

    @ApiOperation(value = "表单数据")
    @GetMapping("/{id}/form")
    public Result<BookRecord> getBookRecordForm(
           @PathVariable Long id
    ) {
        BookRecord formData = bookRecordService.getBookRecordFormData(id);
        return Result.success(formData);
    }


    /**
     * 新增数据
     *
     * @param bookRecord 实体
     * @return 新增结果
     */
    @ApiOperation(value = "新增")
    @PostMapping
    public Result add(
        @RequestBody @Valid
        BookRecord bookRecord
        ) {
        boolean result = this.bookRecordService.saveBookRecord(bookRecord);
        return Result.judge(result);
    }

    @ApiOperation(value = "修改")
    @PutMapping(value = "/{id}")
    public Result updateBookRecord(
            @PathVariable Long id,
            @RequestBody @Validated BookRecord bookRecord) {
        boolean result = bookRecordService.updateBookRecord(id, bookRecord);
        return Result.judge(result);
    }


    @ApiOperation(value = "删除数据")
    @DeleteMapping("/{ids}")
    public Result deleteBookRecords(
             @PathVariable String ids
    ) {
        boolean result = bookRecordService.deleteBookRecords(ids);
        return Result.judge(result);
    }

    @ApiOperation(value = "我的预约记录（实验室id可不传）")
    @GetMapping("/my")
    public Result<List<MyBookRecordVO>> myBookRecord(@RequestParam(value = "labId",required = false) Long labId) {
        List<MyBookRecordVO>  voList = bookRecordService.myBookRecord(labId);
        return Result.success(voList);
    }

}

